const fs = require('fs')
const util = require('./util')
    // const { db } = require('./db')
    // const user = require('./manage/user')
    // const comment = require('./manage/comment')


const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use('/static', express.static(__dirname + '/static'));
app.use(express.static('static')) //挂载静态资源 http://localhost:3001/assets/ocean.jpg

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: "localhost", //ip地址
    port: '3306', //端口号
    user: 'root', //数据库用户
    password: "1356056894", //数据库密码
    database: "oceanweb" //数据库名
});
connection.connect(function(err) {
    if (err) {
        console.log('连接数据库出错:' + err);
        connection.end();
        return;
    };
    console.log('数据库连接成功');
});
//-----------------------------------跳转-------------------------------
app.get('/', function(req, res) {
    res.sendFile(__dirname + "/pages" + "/" + "index.html");
});
app.get('/login', function(req, res) {
    res.sendFile(__dirname + "/pages" + "/" + "login.html");
});
//-----------------------------------业务------------------------------------
app.post("/login", function(req, res) {
        let { name, password } = req.body
        var userGetSql = `SELECT * FROM users WHERE usernum = '${name}' AND password = '${password}'`;
        //查 query
        connection.query(userGetSql, function(err, result) {
            if (err) throw err;
            console.log("result", result)
            if (result.length == 0) {
                console.log(("用户名或密码错误"))
                res.send({
                    code: 0,
                    message: "用户名或密码错误"
                })
            } else {
                console.log(("登录成功"))
                res.send({
                    code: 200,
                    message: "登录成功"
                })
            }
        });
    })
    // 注册
app.post("/register", function(req, res) {
    var { name, password } = req.body
    var userGetSql = `SELECT * FROM users WHERE usernum = '${name}'`;
    var addSql = `INSERT INTO users (is_del,usernum,password) VALUES (0,?,?)`
        //查 query
    connection.query(userGetSql, function(err, result) {
        if (err) {
            console.log('查询出错:' + err);
            return;
        };
        console.log("result", result)
        if (result.length == 0) {
            var inserInfo = [name, password]
            console.log(("注册成功"))
            connection.query(addSql, inserInfo, function(error, result) {
                if (error) {
                    console.log('注册出错:' + error);
                    return;
                };
                res.send({
                    code: 200,
                    message: "注册成功"
                })
            })
        } else {
            console.log(("用户已存在"))
            res.send({
                code: 0,
                message: "用户已存在"
            })
        }
    });
})


app.listen(3001, () => {
    console.log('server is start');
})